<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Aiwangyou
 *
 * @author sunday 2014.05.26 add
 */
require_once 'BaseChannel.php';

//91平台服务器端接入
class NightOne implements BaseChannel {

	//put your code here
	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($data) {
		$appId="115005";
		$appKey = "9b9d0d3e9ac97e2e10d5defb011438b6ef32c7e6d495f443";
		echo $this->paymentCallback($appId, $appKey, ANDROID);
		 
	}

	function ios($data) {
		//var_dump($data);
		$appId="114468";
		$appKey = "1fefaeccf0a4048ba8967d2119b87777886f0997f1daf155";
		echo $this->paymentCallback($appId, $appKey, IOS);
	}

	function paymentCallback($MyAppId,$MyAppKey,$platform)
	{
		$Result = array();//存放结果数组
		$orderLog = new ChargeOrder();
		$orderLog->log("91", $_REQUEST);
		//print_r($_GET);		
		//var_dump($_REQUEST);
		if(empty($_GET)||!isset($_GET['AppId'])||!isset($_GET['Act'])||!isset($_GET['ProductName'])||!isset($_GET['ConsumeStreamId'])
		||!isset($_GET['CooOrderSerial'])||!isset($_GET['Uin'])||!isset($_GET['GoodsId'])||!isset($_GET['GoodsInfo'])||!isset($_GET['GoodsCount'])||!isset($_GET['OriginalMoney'])
		||!isset($_GET['OrderMoney'])||!isset($_GET['Note'])||!isset($_GET['PayStatus'])||!isset($_GET['CreateTime'])||!isset($_GET['Sign'])){

			$Result["ErrorCode"] =  "0";//注意这里的错误码一定要是字符串格式
			$Result["ErrorDesc"] =  urlencode("reveive error");
			$Res = json_encode($Result);
			$orderLog->log("91"," reveive data error, sth is empty ");
			//echo " receive error ";
			return urldecode($Res);
		}

		$AppId 				= $_GET['AppId'];//应用ID
		$Act	 			= $_GET['Act'];//操作
		$ProductName		= $_GET['ProductName'];//应用名称
		$ConsumeStreamId	= $_GET['ConsumeStreamId'];//消费流水号
		$CooOrderSerial	 	= $_GET['CooOrderSerial'];//商户订单号
		$Uin			 	= $_GET['Uin'];//91帐号ID
		$GoodsId		 	= $_GET['GoodsId'];//商品ID
		$GoodsInfo		 	= $_GET['GoodsInfo'];//商品名称
		$GoodsCount		 	= $_GET['GoodsCount'];//商品数量
		$OriginalMoney	 	= $_GET['OriginalMoney'];//原始总价（格式：0.00）
		$OrderMoney		 	= $_GET['OrderMoney'];//实际总价（格式：0.00）
		$Note			 	= $_GET['Note'];//支付描述
		$PayStatus		 	= $_GET['PayStatus'];//支付状态：0=失败，1=成功
		$CreateTime		 	= $_GET['CreateTime'];//创建时间
		$Sign		 		= $_GET['Sign'];//91服务器直接传过来的sign


		//因为这个DEMO是接收验证支付购买结果的操作，所以如果此值不为1时就是无效操作
		if($Act != 1){
			$Result["ErrorCode"] =  "3";//注意这里的错误码一定要是字符串格式
			$Result["ErrorDesc"] =  urlencode("Act无效");
			$Res = json_encode($Result);
			$orderLog->log("91", " act error act = $Act ");
			return urldecode($Res);
		}

		//如果传过来的应用ID开发者自己的应用ID不同，那说明这个应用ID无效
		if($MyAppId != $AppId){
			$Result["ErrorCode"] =  "2";//注意这里的错误码一定要是字符串格式
			$Result["ErrorDesc"] =  urlencode("AppId无效");
			$Res = json_encode($Result);			
			$orderLog->log("91", " appid not match $MyAppId != $AppId ");
			
			return urldecode($Res);
		}

		//按照API规范里的说明，把相应的数据进行拼接加密处理
		$sign_check = md5($MyAppId.$Act.$ProductName.$ConsumeStreamId.$CooOrderSerial.$Uin.$GoodsId.$GoodsInfo
						.$GoodsCount.$OriginalMoney.$OrderMoney.$Note.$PayStatus.$CreateTime.$MyAppKey);

		if($sign_check == $Sign){//当本地生成的加密sign跟传过来的sign一样时说明数据没问题
			
			/*
			 *
			 * 开发者可以在此处进行订单号是否合法、商品是否正确等一些别的订单信息的验证处理
			 * 相应的别的错误用不同的代码和相应说明信息，数字和信息开发者可以自定义（数字不能重复）
			 * 如果所有的信息验证都没问题就可以做出验证成功后的相应逻辑操作
			 * 不过最后一定要返回上面那样格式的json数据
			 *
			 */
			if(0 == $PayStatus)	//支付状态：0=失败，1=成功
			{
				$status = STATUS_FAILD;
			}else 
			{
				$status = STATUS_SUCCESS;
			}
			
			if(true == $orderLog->recordOrder($status, $OrderMoney, $CooOrderSerial, "91", $platform,$ConsumeStreamId))
			{
				//成功支付
	                $Result["ErrorCode"] =  "1";//注意这里的错误码一定要是字符串格式
					$Result["ErrorDesc"] =  urlencode(" success");
					
					$orderLog->log("91", array("platform"=>$platform,"status"=>$status,"CooOrderSerial"=>$CooOrderSerial,
								"OrderMoney"=>$OrderMoney,"CreateTime"=>$CreateTime,"gameserverresult"=>'success'));
					//echo "success";
			}
	        else
	        {
	        		//6表示 插入数据库中失败
	                $Result["ErrorCode"] =  "6";//注意这里的错误码一定要是字符串格式
					$Result["ErrorDesc"] =  urlencode(" sql insert faild");
					
					$orderLog->log("91", array("platform"=>$platfrom,"status"=>$status,"CooOrderSerial"=>$CooOrderSerial,
								"OrderMoney"=>$OrderMoney,"CreateTime"=>$CreateTime,"gameserverresult"=>'failed'));					
					//echo " insert data to server error : status:$status money:$OrderMoney orderid:$CooOrderSerial platform:$platform ";

	        }   
	
			$Res = json_encode($Result);
			//print_r($Result);
			return urldecode($Res);
		}
		else
		{
			$Result["ErrorCode"] =  "5";//注意这里的错误码一定要是字符串格式
			$Result["ErrorDesc"] =  urlencode(" Sign invalid");
			$Res = json_encode($Result);
			//print_r($Result);
			//echo " order:$CooOrderSerial Sign invalid ";
			return urldecode($Res);
		}
	}

}

?>
